﻿<!DOCTYPE HTML>
<html lang="zh">
<head>
<title>HotIf - 语法 &amp; 使用 | AutoHotkey v2</title>
<meta name="description" content="The HotIf function specifies the criteria for subsequently created or modified hotkey variants." />
	<meta name="ahk:equiv-v1" content="commands/Hotkey.htm" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
<script type="text/javascript">$(function(){0<=window.navigator.userAgent.toLowerCase().indexOf("ucbrowser")&&CaoNiMaDeUc()})</script>
</head>
<body>

<h1>HotIf / HotIfWin</h1>

<p>指定后续创建或修改热键<a href="Hotkey.htm#variant">变体</a>的条件.</p>
<p>目录:</p>
<ul>
  <li><a href="#If">HotIf</a></li>
  <li><a href="#IfWin">HotIfWin...</a></li>
  <li><a href="#remarks">备注</a></li>
  <li><a href="#examples">示例</a></li>
</ul>

<h2 id="If">HotIf</h2>
<pre class="Syntax">
<span class="func">HotIf</span> <span class="optional">"Expression"</span>
<span class="func">HotIf</span> <span class="optional">FunctionObject</span>
</pre>

<h3 id="Parameters">参数</h3>
<dl>

  <dt id="if-blank">(省略参数)</dt>
  <dd><p>设置空白条件(关闭上下文相关性).</p></dd>

  <dt id="if-expr">"Expression"</dt>
  <dd>
    <p>为现有的 <a href="_HotIf.htm">#HotIf</a> 表达式设置条件. <em>Expression</em> 通常写成一个<a href="../Language.htm#strings">加引号字符串</a>, 但也可以是一个变量或表达式, 返回与 #HotIf 表达式匹配的文本. 虽然这个函数不能创建新的表达式, 但它可以使用现有的表达式创建新的热键. 请参阅 <a href="_HotIf.htm#ExDynamic">#HotIf 示例 4</a>.</p>
    <p class="note"><strong>注意</strong>: HotIf 函数使用你传递给它的字符串, 而不是原始源代码. 当脚本加载时, <a href="../misc/EscapeChar.htm">转义序列</a>会被解析, 所以只考虑结果字符; 例如, <code>HotIf 'x = "`t"'</code> 和 <code>HotIf 'x = "' A_Tab '"'</code> 都对应于 <code>#HotIf x = "`t"</code>.</p>
  </dd>

  <dt id="if-func">FunctionObject</dt>
  <dd>
    <p>Sets the criteria to a given <a href="../objects/Functor.htm">function object</a>. Subsequently-created hotkeys will only execute if calling the given function object yields a non-zero number. This is like <code>HotIf "Expression"</code>, except that each hotkey can have many <a href="Hotkey.htm#variant">variants</a> (one per object). <em>FunctionObject</em> must be an object with a <em>call</em> method taking one parameter, the <a href="../Hotkeys.htm#HotBraces">name</a> of the hotkey.</p>
    <p>Once passed to the HotIf function, the object will never be deleted (but memory will be reclaimed by the OS when the process exits).</p>
    <p>The <a href="Hotkey.htm#ExampleIfFn">"three-key combination" Hotkey example</a> uses this mode of HotIf.</p>
  </dd>

</dl>

<h2 id="IfWin">HotIfWin...</h2>
<pre class="Syntax">
<span class="func">HotIfWinActive</span> <span class="optional">WinTitle, WinText</span>
<span class="func">HotIfWinExist</span> <span class="optional">WinTitle, WinText</span>
<span class="func">HotIfWinNotActive</span> <span class="optional">WinTitle, WinText</span>
<span class="func">HotIfWinNotExist</span> <span class="optional">WinTitle, WinText</span>
</pre>

<h3 id="Parameters_3">参数</h3>
<dl>

  <dt>(all parameters omitted)</dt>
  <dd><p>Sets blank criteria (turns off context-sensitivity).</p></dd>

  <dt>WinTitle, WinText</dt>
  <dd>
    <p>Specifies the window title and other criteria that should be used to identify a window. Depending on which function is called, affected hotkeys and hotstrings are active only while a matching window is active, exists, is not active, or does not exist.</p>
    <p>Since the parameters are evaluated before the function is called, any variable reference becomes permanent at that moment. In other words, subsequent changes to the contents of the variable are not seen by existing hotkeys.</p>
    <p><em>WinTitle</em> and <em>WinText</em> have the same meaning as for <a href="WinActive.htm">WinActive</a> or <a href="WinExist.htm">WinExist</a>, but they use the default settings for <a href="SetTitleMatchMode.htm">SetTitleMatchMode</a> and <a href="DetectHiddenWindows.htm">DetectHiddenWindows</a> as set by the <a href="../Scripts.htm#auto">auto-execute thread</a>. See <a href="../misc/WinTitle.htm">WinTitle</a> for details.</p>
  </dd>

</dl>

<h2 id="Error_Handling">错误处理</h2>
<p>An exception is thrown if HotIf's parameter is invalid, such as if it does not match an existing expression and is not a valid callback function.</p>

<h2 id="remarks">备注</h2>
<p>HotIf allows context-sensitive <a href="../Hotkeys.htm">hotkeys</a> to be created and modified while the script is running (by contrast, the <a href="_HotIf.htm">#HotIf</a> directive is positional and takes effect before the script begins executing). For example:</p>
<pre>HotIfWinActive "ahk_class Notepad"
Hotkey "^!e", "MyLabel"  <em>; Creates a hotkey that works only in Notepad.</em></pre>
<p>Using HotIf puts context sensitivity into effect for all subsequently created <a href="../Hotkeys.htm">hotkeys</a> in the current <a href="../misc/Threads.htm">thread</a>, and affects which hotkey variants the <a href="Hotkey.htm">Hotkey</a> function modifies. Only the most recent call to any of the HotIf functions in the current thread will be in effect.</p>
<p>To turn off context sensitivity (such as to make subsequently-created hotkeys work in all windows), call any HotIf function but omit the parameters. For example: <code>HotIf</code> or <code>HotIfWinActive</code>.</p>
<p>Before HotIf is used in a hotkey or hotstring <a href="../misc/Threads.htm">thread</a>, the Hotkey and Hotstring functions default to the same context as the hotkey or hotstring that launched the thread. In other words, <code>Hotkey A_ThisHotkey, "Off"</code> turns off the current hotkey even if it is context-sensitive. All other threads default to creating or modifying global hotkeys, unless that default is overridden by using HotIf during <a href="../Scripts.htm#auto">script startup</a>.</p>
<p>When a mouse or keyboard hotkey is disabled via a HotIf function or directive, it performs its native function; that is, it passes through to the active window as though there is no such hotkey. However, joystick hotkeys always pass through, whether they are disabled or not.</p>

<h2 id="Related">相关</h2>
<p><a href="Hotkey.htm">Hotkey</a> (function), <a href="../Hotkeys.htm">Hotkeys</a> (general), <a href="_HotIf.htm">#HotIf</a>, <a href="../misc/Threads.htm">Threads</a></p>

<h2 id="examples">示例</h2>
<p>See <a href="Hotkey.htm#ExOther">Hotkey</a> for examples.</p>

</body>
</html>